let { projectRootPath } = require("../../util/path/index.js");
let {
  executeSql,
  transaction,
  getTotal,
  getByPagination,
  removeByUuid,
  getAll,
} = require(`${projectRootPath}/mysql/index.js`);
let { getUuid } = require(`${projectRootPath}/util/uuid/index.js`);
let { getCurrentDatetime } = require(
  `${projectRootPath}/util/datetime/index.js`,
);
let { getUserByAll, editUserByUuid } = require(
  `${projectRootPath}/service/user/index.js`,
);

// 创建缴费
async function createPay(data) {
  let { ownerUuid, attachment } = data;
  return await transaction(async (connection) => {
    let currentDatetime = getCurrentDatetime();
    let sql = `insert into PayTable (uuid, ownerUuid, attachment, sendDatetime, opDatetime) values ('${getUuid()}', '${ownerUuid}', '${attachment}', '${currentDatetime}', '${currentDatetime}')`;
    await executeSql(connection, sql);
  });
}

// 获取缴费分页数据
async function getPayByPagination(data) {
  let { pageSize, currentPage, sendDateRange, ownerUuid } = data;
  let whereAll = [];
  if (sendDateRange.length === 1) {
    let where = `sendDatetime between '${sendDateRange[0]} 00:00:00' and '${sendDateRange[0]} 23:59:59'`;
    whereAll.push(where);
  } else if (sendDateRange.length === 2) {
    let where = `sendDatetime between '${sendDateRange[0]} 00:00:00' and '${sendDateRange[1]} 23:59:59'`;
    whereAll.push(where);
  }
  if (ownerUuid) {
    whereAll.push(`ownerUuid='${ownerUuid}'`);
  }
  if (whereAll.length === 0) {
    whereAll = "";
  } else {
    whereAll = "where " + whereAll.join(" and ");
  }

  return await transaction(async (connection) => {
    let list = await getByPagination(
      connection,
      "PayTable",
      "opDatetime",
      whereAll,
      pageSize,
      currentPage,
    );
    let total = await getTotal(connection, "PayTable", "opDatetime", whereAll);
    return { list, total };
  });
}

// 获取条件下的全部缴费
async function getAllPayByFormdata(data) {
  let { sendDateRange, ownerUuid } = data;
  let whereAll = [];
  if (sendDateRange.length === 1) {
    let where = `sendDatetime between '${sendDateRange[0]} 00:00:00' and '${sendDateRange[0]} 23:59:59'`;
    whereAll.push(where);
  } else if (sendDateRange.length === 2) {
    let where = `sendDatetime between '${sendDateRange[0]} 00:00:00' and '${sendDateRange[1]} 23:59:59'`;
    whereAll.push(where);
  }
  if (ownerUuid) {
    whereAll.push(`ownerUuid='${ownerUuid}'`);
  }
  if (whereAll.length === 0) {
    whereAll = "";
  } else {
    whereAll = "where " + whereAll.join(" and ");
  }

  return await transaction(async (connection) => {
    let currentPage = 1;
    let pageSize = 20;
    let count = 0;
    let result = [];
    do {
      let list = await getByPagination(
        connection,
        "PayTable",
        "opDatetime",
        whereAll,
        pageSize,
        currentPage,
      );
      if (list.length > 0) {
        result = [...result, ...list];
      }
      count = list.length;
      currentPage = currentPage + 1;
    } while (count === pageSize);

    return { list: result };
  });
}

module.exports = {
  createPay,
  getPayByPagination,
  getAllPayByFormdata,
};
